package com.xiaobai.myblog.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaobai.myblog.common.lang.Result;
import com.xiaobai.myblog.entity.Expend;
import com.xiaobai.myblog.service.ExpendService;
import com.xiaobai.myblog.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class ExpendController {
    @Autowired
    private ExpendService expendService;

    @GetMapping("/expend/list")
    public Result list(@RequestBody Map<String, String> requestMap) {
        System.out.print(requestMap);
        QueryWrapper queryWrapper = new QueryWrapper<Expend>();
        if (requestMap.get("uId")!=null) {
            queryWrapper.eq("u_id", requestMap.get("uId"));
        }
        if (requestMap.get("type")!=null) {
            queryWrapper.eq("type", requestMap.get("type"));
        }
        if (requestMap.get("beforeDate")!=null && requestMap.get("afterDate") != null) {
            queryWrapper.between("date", requestMap.get("beforeDate"), requestMap.get("afterDate"));
        }
        queryWrapper.orderByDesc("date");
        List<Expend> list = expendService.list(queryWrapper);

        // 处理数据
        HashMap<String, ArrayList> resultMap = new HashMap<String, ArrayList> ();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        ArrayList<Object> temp = null;
        for (Expend expend:list) {
            String formatDate = sdf.format(expend.getDate());
            if (resultMap.get(formatDate) != null) {
                temp = resultMap.get(formatDate);
                temp.add(expend);
            } else {
                temp = new ArrayList<>();
                temp.add(expend);
            }
            resultMap.put(formatDate, temp);
        }
        return Result.succ(resultMap);
    }

    @PostMapping("/expend/edit")
    public Result edit(@Validated @RequestBody Expend expend) {
        Expend temp = null;
        if (expend.getId() != null) {
            temp = expendService.getById(expend.getId());
            // 只能编辑自己的费用单
            Assert.isTrue(temp.getId().longValue() == ShiroUtil.getProfile().getId().longValue(), "没有权限编辑");
        } else {
            temp = new Expend();
            temp.setCreateTime(new Date());
            temp.setUId(ShiroUtil.getProfile().getId());
        }
        temp.setUpdateTime(new Date());
        BeanUtil.copyProperties(expend, temp, "id", "uId" ,"updateTime", "createTime");
        expendService.saveOrUpdate(temp);
        return Result.succ(null);
    }
}
